package com.ruoyi.quartz.task;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.domain.AInventory;
import com.ruoyi.quartz.domain.DrugsNotice;
import com.ruoyi.quartz.service.AIInventoryService;
import com.ruoyi.quartz.service.IDrugsNoticeService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.impl.SysNoticeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("DrugTask")
public class DrugTask {

    @Autowired
    private IDrugsNoticeService drugsNoticeService;


    @Autowired
    private AIInventoryService aiInventoryService;


    @Autowired
    private SysNoticeServiceImpl sysNoticeService;




    /**
     * 定时任务调度测试
     *
     * @author ruoyi
     */
    public void ryParams() {


        DrugsNotice drugsNotice = new DrugsNotice();
        AInventory inventory = new AInventory();
        SysNotice sysNotice = new SysNotice();
        System.out.println("任务开始");
        //查看预警表
        List<DrugsNotice> list = drugsNoticeService.selectDrugsNoticeList(drugsNotice);
        if (list != null) {
            for (DrugsNotice notice : list) {
                //获取drugId，和预警数量，去库存表查询库存数量
                long drugId = notice.getDrugId();
                long stockQuantity = notice.getStockQuantity();
                inventory.setDrugId(drugId);
                List<AInventory> inventoryList = aiInventoryService.selectInventoryList(inventory);
                if (inventoryList != null) {
                    inventoryList.forEach(res -> {
                        long inventoryQ = res.getStockQuantity();
                        if (stockQuantity>=inventoryQ){
                            //新增公告
                            sysNotice.setNoticeTitle("库存预警");
                            sysNotice.setNoticeType("1");
                            sysNotice.setNoticeContent("<p>库存ID："+res.getInventoryId()+"的药品:"+notice.getDrugName()+"的库存已不足,请及时补货,-库存:"+inventoryQ+"</p>");
                            sysNotice.setCreateBy("admin");
                            sysNotice.setStatus(String.valueOf(0));
                            int code=sysNoticeService.insertNotice(sysNotice);
                        }
//                    }
                    });
                }





                System.out.println("执行无参方法");

            }
        }
    }
}
